package com.drama.api.sys.organization.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.sys.organization.dao.SysOrganizationDao;
import com.drama.api.sys.organization.domain.po.SysOrganizationPO;
import com.drama.api.sys.organization.domain.vo.*;
import com.drama.api.sys.organization.service.ISysOrganizationService;
import com.drama.api.sys.user.dao.SysUserDao;
import com.drama.api.sys.user.domain.po.SysUserPO;
import com.drama.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
@Transactional
public class SysOrganizationServiceImpl implements ISysOrganizationService {

    @Autowired
    SysOrganizationDao dao;
    @Autowired
    SysUserDao sysUserDao;

    @Override
    public List<SysOrganizationVO> getList(SysOrganizationSearchVO searchVO) {
        //设置当前用户机构信息 查询权限机构数据
//        SysUserPO sysUserPO=sysUserDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
//        searchVO.setSysUserOrgId(sysUserPO.getOrganizationId());
        return dao.getList(searchVO);
    }

    @Override
    public List<SysOrganizationVO> getTreeList(SysOrganizationSearchTreeVO searchVO) {
        //设置当前用户机构信息 查询权限机构数据
//        SysUserPO sysUserPO=sysUserDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
//        searchVO.setSysUserOrgId(sysUserPO.getOrganizationId());
        List<SysOrganizationVO> list=dao.getTreeList(searchVO);
        List<SysOrganizationVO> rList = new ArrayList<>();
        HashMap<String, SysOrganizationVO> map = new HashMap<>();
        for (SysOrganizationVO vo : list) {
            map.put(vo.getId(), vo);
        }
        for (SysOrganizationVO vo : list) {
            if (vo.getParentId() == null) {
                continue;
            }
            if (vo.getParentId().equals(BigDecimal.ZERO.toString())) {
                rList.add(vo);
            } else {
                SysOrganizationVO parentVO = map.get(vo.getParentId());
                if (parentVO == null) {
                    rList.add(vo);
                    continue;
                }
                if (parentVO.getChildren() == null) {
                    parentVO.setChildren(new ArrayList<>());
                }
                parentVO.getChildren().add(vo);
            }
        }
        return rList;
    }

    @Override
    public String add(SysOrganizationAddVO addVO) {
        SysOrganizationPO po=new SysOrganizationPO();
        BeanUtils.copyProperties(addVO, po);
        po.preInsert();
        if (StringUtils.isNotBlank(po.getParentId()) && !po.getParentId().equals(BigDecimal.ZERO.toString())) {
            setOrgParentId(po);
        }
        if(StringUtils.isBlank(po.getParentId())){
            po.setParentId(BigDecimal.ZERO.toString());
            po.setParentIds(po.getParentId()+","+po.getId());
        }
        int i = dao.insertSelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
//        SysLogUtils.addOpLog(new BaseEntity().getUser().getId(),"添加-机构信息-"+ JSON.toJSONString(addVO),po.getId());
        return po.getId();
    }

    @Override
    public void update(SysOrganizationUpdateVO updateVO) {
        SysOrganizationPO po=new SysOrganizationPO();
        BeanUtils.copyProperties(updateVO, po);
        po.preUpdate();
        if (StringUtils.isNotBlank(po.getParentId())) {
            setOrgParentId(po);
        }
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
//        SysLogUtils.addOpLog(new BaseEntity().getUser().getId(),"编辑-机构信息-"+  JSON.toJSONString(updateVO),po.getId());
    }

    private void setOrgParentId(SysOrganizationPO po) {
        SysOrganizationPO organizationPO = dao.selectByPrimaryKey(po.getParentId());
        if (organizationPO == null) {
            throw new BusinessException(Type.FAIL);
        }
        po.setParentIds(organizationPO.getParentIds()+ "," + po.getId() + ",");
    }

    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.FORMAT_EXCEPTION);
        }
        SysOrganizationPO po=new SysOrganizationPO();
        SysUserPO sysUserPO=sysUserDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if(sysUserPO.getOrganizationId().equals(id)){
            throw new BusinessException(Type.FAIL);
        }
        po.setId(id);
        po.preDelete();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
        SysOrganizationPO opJson = new SysOrganizationPO();
        opJson.setId(id);
//        SysLogUtils.addOpLog(new BaseEntity().getUser().getId(),"删除-机构信息-"+ JSON.toJSONString(opJson),po.getId());
    }
}
